import krister.Ess.*;

AudioChannel myChannel;
FFT myFFT;
int bands = 256; // Number of FFT frequency bands to calculate
int bufferSize;
int bufferDuration;
PImage dropimage;
int localPix = width*height;
int bandpos = width + 1;


void setup() {
  frameRate(100);
  size(1024, 256);
  colorMode(HSB);
  Ess.start(this); // Start Ess
  //File loaded must be in the "data" folder
  myChannel = new AudioChannel("hello_dave.wav");
  bufferSize=myChannel.buffer.length;
  bufferDuration=myChannel.ms(bufferSize);
  myChannel.play(Ess.FOREVER);
  myFFT = new FFT(bands * 2); // We want 256 frequency bands, so we pass in 512
}

void draw() {

  //background(176);

  // Get spectrum
  myFFT.getSpectrum(myChannel);


  // Draw FFT data
  stroke(255);
  loadPixels();
  
   
    //int j = 0;
      copy(0,0, width-1, height, 1,0, width, height);
      for(int i = 0; i < height ; i++) {
        float maxspect = max(0,5*myFFT.spectrum[i]*2048);
        println(maxspect);
        color spect = color (maxspect, 50+maxspect, maxspect);
        pixels[i*width] =  spect;        
      } 
      updatePixels();      
        
       
  
  //Ess.stop();
  /*
  while (i < height) {
   float y = height - pow(1024, (255.0 - i) / bands);
   float maxX = max(0, myFFT.maxSpectrum[i] * 2);
   float freX = max(0, myFFT.spectrum[i] * 2);
   stroke(255);
   stroke(0);
   for (int j = 0; j <bands;i++) {
   point(j, freX);
   }
   i++;
   }
   */
}

void stop() {
  Ess.stop(); // When program stops, stop Ess too
  super.stop();
}


void loadFile(String filename) {
  Ess.stop();
  Ess.start(this);
  myChannel = new AudioChannel(filename);
  //dropimage = loadImage(filename);
  println(filename);
  bufferSize=myChannel.buffer.length;
  bufferDuration=myChannel.ms(bufferSize);
  myChannel.play(Ess.FOREVER);
} 
